import { fileURLToPath, URL } from "node:url";

import { defineConfig, loadEnv, type ConfigEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";

import viteSvgLoader from "vite-svg-loader";

import mkcert from "vite-plugin-mkcert";

export default ({ mode }: ConfigEnv) => {
	// const VITE_APP_SERVER_URL: string = loadEnv(mode, process.cwd()).VITE_APP_SERVER_URL;
	console.log("BUILDING GVIEW WITH MODE", mode);

	return defineConfig({
		base: mode === "springboot" ? "./" : undefined,
		plugins: [vue(), vueJsx(), viteSvgLoader()],
		resolve: {
			alias: {
				"@": fileURLToPath(new URL("./src", import.meta.url)),
				"~": fileURLToPath(new URL("../packages", import.meta.url)),
				"@gonsin/gview": fileURLToPath(new URL("../packages", import.meta.url)),
			},
		},
		css: {
			/* CSS 预处理器 */
			preprocessorOptions: {
				scss: {
					additionalData: '@import "~/styles/index.scss";',
				},
			},
		},
		server: {
			port: 9999,
			proxy: {
				"/api": {
					target: "http://192.168.2.166",
				},
				"/restapi": {
					target: "http://192.168.2.166",
				},
				"/gview/websocket": {
					target: "ws://192.168.2.166",
				},
				"/temp": {
					target: "http://192.168.2.166",
				},
				"/files": {
					target: "http://192.168.2.166",
				},
				"/lang": {
					target: "http://192.168.2.166",
				},
				"/license": {
					target: "http://192.168.2.166",
				},
			},
		},
	});
};
